<?php

	/* dependence */
	CoreProject::Using('core.utils');
	
	/**
	 * @package		CoreProject
	 * @subpackage	Core
	 *
	 * cpr_Core_Log
	 * 
	 */

	class cpr_Core_Log
	{
		const LOG_LIMIT_WRITE = 4000;
		const LOG_TIMERUN = 200;
		const LOG_CUT_TEXT = ' ----------- cut ------------ ';

		private function __construct() {}
		
		/**
		 * Example:
		 * <code>
		 *		cpr_Core_Log::Write('Error: unknown error.', '/var/tmp/cpr_file.log', '4.2.3', __FILE__, __LINE__);
		 * </code>
		 * 
		 * @param	string	$_errorDesc
		 * @param	string	$_version = ''
		 * @param	string	$_file = ''
		 * @param	string	$_line = ''
		 * @return	bool
		 */
		public static function Write($_errorDesc, $_version = '', $_file = '', $_line = '')
		{
			if (!defined('CPR_LOG_FILE'))
			{
				return true;
			}

			if ('' === $_version)
			{
				$_version = defined('CPR_LOG_VERSION') ? CPR_LOG_VERSION : '';
			}

			static $_isFirst = true;
			static $_lastTime = null;
			
			$_time = time();
			if ($_isFirst)
			{
				$_isFirst = false;
				$_ftime = date('d/m/Y H:i:s', $_time);
				$_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
				$_server = isset($_SERVER['SERVER_SOFTWARE']) ? ', Server: '.$_SERVER['SERVER_SOFTWARE'] : '';
				$_post = (isset($_POST) && count($_POST) > 0) ? ' [POST('.count($_POST).')]' : '';
				$_version = strlen($_version) > 0 ? 'ver: '.$_version.', ' : '';
				@error_log(
					CPR_CRLF.'['.$_ftime.'.'.((int) (self::_getMicroSec() * 1000)).'] --------------------------------------------- '.
					CPR_CRLF.'   URL  > '.$_uri.$_post.
					CPR_CRLF.'   INFO > '.$_version.'PHP: '.phpversion().$_server.
					CPR_CRLF, 3, CPR_LOG_FILE);
				
				if (strlen($_post) > 0)
				{
					@error_log('POST > ['.implode(', ', array_keys($_POST)).']'.CPR_CRLF, 3, CPR_LOG_FILE);
				}
			}
			
			if (self::LOG_LIMIT_WRITE && strlen($_errorDesc) > self::LOG_LIMIT_WRITE * 2)
			{
				$_errorDesc =
					substr($_errorDesc, 0, self::LOG_LIMIT_WRITE).CPR_CRLF.
					self::LOG_CUT_TEXT.CPR_CRLF.
					substr($_errorDesc, -ceil(self::LOG_LIMIT_WRITE / 2));
			}

			$_fileline = '';
			if (strlen($_file) > 0)
			{
				$_fileline = ' [file: '.basename($_file);
				if (strlen($_line) > 0)
				{
					$_fileline .= ' (~'.$_line.')';
				}
				$_fileline .= ']';
			}

			$_newTime = self::_getMicroTime();
			$_newSTime = (int) (self::_getMicroSec() * 1000);
			$_pref = '';
			if (null !== $_lastTime)
			{
				$_tmpTime = (int) (($_newTime - $_lastTime) * 1000);
				if ($_tmpTime > self::LOG_TIMERUN)
				{
					$_pref = $_tmpTime.'-';
				}
			}
			$_lastTime = $_newTime;
			return @error_log($_pref.'['.date('H:i:s', $_newTime).'.'.$_newSTime.']'.$_fileline.' '.$_errorDesc.CPR_CRLF, 3, CPR_LOG_FILE);
		}

		/**
		 * @return	bool
		 */
		public static function Clear()
		{
			if (!defined('CPR_LOG_PATH'))
			{
				return true;
			}
			return @unlink(CPR_LOG_PATH);
		}

		/**
		 * @return float
		 */
		private static function _getMicroSec()
		{
	    	$usec = explode(' ', microtime());
	    	return isset($usec[0]) ? (float) $usec[0] : -1;
		}
		
		/**
		 * @return float
		 */
		private static function _getMicroTime()
		{
	    	return microtime(true);
		}
	}
